<template>
    <el-form ref="form" :model="form" label-width="80px" size="mini" :rules="rules">
        <label >用户反馈</label><br></br>

        <!-- <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" ></el-input>
        </el-form-item> -->
        <el-form-item label="内容" prop="content">
            <el-input type="textarea" v-model="form.content" size="large"></el-input>
        </el-form-item>
        
        <el-form-item label="添加图片"  ref="upload">
            <el-upload
                action="http://zbfc.jshyjn.cn/common/upload"
                list-type="picture-card"
                :before-upload="beforeUpload"
                accept=".jpg,.jpeg,.png"
                :limit="1"
                :on-exceed="handleExceed"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :on-success="handleSucess">
                <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="">
            </el-dialog>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">立即反馈</el-button>
            <el-button>取消</el-button>
        </el-form-item>
    </el-form>
</template>


<script>
import feedbackApi from '@/api/feedback'
import axios from 'axios'

export default {
    
    data() {
      return {
        form: {
          title: '',
          content: '',
          fileUrl:''
          // fileUrls:[]
        },
        files:[],
        // fileUrls:[],
        dialogImageUrl: '',
        dialogVisible: false,
        BASE_API: process.env.BASE_API,
        rules:{
            title: [
                { required: true, message: '请输入标题', trigger: 'blur' },
                { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
            ],
            content: [
                { required: true, message: '请输入消息内容', trigger: 'blur' },
                { min: 3, max: 20, message: '长度在 3 到 200 个字符', trigger: 'blur' }
            ],
        }
      }
    },
    methods: {
      onSubmit() {
        // this.form.fileUrls=this.fileUrls
        console.log('submit!---',this.form.fileUrl,this.form.title,this.form.content);

        this.$refs['form'].validate((valid) => {
          if (valid) {
            // feedbackApi.feedback(this.form)
            //     .then(response=>{
            //     // 提示信息
            //     this.$message({
            //       type: 'success',
            //       message: '反馈成功!'
            //     })
            //     // this.$route.go(0)
            // })

            let url=this.BASE_API+'/system/message/add'

            const fd=new FormData()
            fd.append('content',this.form.content)
            fd.append('img',this.form.fileUrl)
            
            axios.post(url, fd, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}).then(res => {
              if (res.data.code === 0) {
                // 提示信息
                this.$message({
                  type: 'success',
                  message: '反馈成功!'
                })
                this.$refs['form'].resetFields()
                // this.$router.go(-1)
              }
            }).catch(error => {
              alert('反馈失败' + error)
            })

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      // 删除
      handleRemove(file, fileList) {
        console.log(file, fileList)
        

      },
      // 上传预览
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
        console.log(this.dialogImageUrl)
      },
      // 上传成功   
      handleSucess(response, file, fileList){
        this.form.fileUrl=response.url;

        console.log(response)
        // console.log(JSON.stringify(file.url))
        // console.log(JSON.stringify(file))
        // console.log(JSON.stringify(fileList))
        // console.log(JSON.stringify(this.fileUrls))
      },
      // 文件数量超过限制提示  
      handleExceed(files,fileList){
        this.$message({
            message: '最大上传数量不能超过1个',
            type: 'warning'
        });
      },

      // 上传文件之前
      beforeUpload(file) {
        const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);
        
        const whiteList = ["jpg", "jpeg", "png"];
        
        if (whiteList.indexOf(fileSuffix) === -1) {
            this.$message({
            message: '上传文件只能是.jpg,.jpeg,.png格式',
            type: 'warning'
        });
        return false;
      }
        
        const isLt2M = file.size / 1024 / 1024 < 2;
        
        if (!isLt2M) {
            this.$message({
            message: '上传文件大小不能超过 2MB',
            type: 'warning'
        });
            return false;
        }
        },

    }
}
</script>

<style>

</style>

